import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:thinking/entity/_.dart';
import 'TileWidget.dart';

class StaggeredGridViewPage extends StatelessWidget {
   StaggeredGridViewPage.count({
    required this.title,
    required this.crossAxisCount,
    required this.tiles,
    required this.thinkDataList,
    this.mainAxisSpacing: 4.0,
    this.crossAxisSpacing: 4.0,
  })  : _staggeredGridViewMode = _StaggeredGridViewMode.count,
        maxCrossAxisExtent = null;

   StaggeredGridViewPage.extent({
    required this.title,
    required this.maxCrossAxisExtent,
    required this.tiles,
     required this.thinkDataList,
     this.mainAxisSpacing: 4.0,
    this.crossAxisSpacing: 4.0,
  })  : _staggeredGridViewMode = _StaggeredGridViewMode.extent,
        crossAxisCount = null;

  static const EdgeInsetsGeometry padding =
  const EdgeInsets.symmetric(horizontal: 4.0);

  final String title;
  final List<StaggeredTile> tiles;
  final List<Think> thinkDataList;
  final dynamic crossAxisCount;
  final double mainAxisSpacing;
  final double crossAxisSpacing;
  final dynamic maxCrossAxisExtent;
  final _StaggeredGridViewMode _staggeredGridViewMode;
  final ScrollController _scrollController = ScrollController();

  @override
  Widget build(BuildContext context) {
    return Container(
            margin:  EdgeInsets.only(top: 4.0),
            child: _buildStaggeredGridView(context));
  }

  Widget _buildStaggeredGridView(BuildContext context) {
    switch (_staggeredGridViewMode) {
      case _StaggeredGridViewMode.count:
        return  StaggeredGridView.countBuilder(
          shrinkWrap: true,
          controller: _scrollController,
          crossAxisCount: crossAxisCount,
          itemCount: tiles.length,
          itemBuilder: _getChild,
          mainAxisSpacing: mainAxisSpacing,
          crossAxisSpacing: crossAxisSpacing,
          padding: padding,
          staggeredTileBuilder: _getStaggeredTile,
        );
      default:
        return  StaggeredGridView.extentBuilder(
          maxCrossAxisExtent: maxCrossAxisExtent,
          itemCount: tiles.length,
          itemBuilder: _getChild,
          mainAxisSpacing: mainAxisSpacing,
          crossAxisSpacing: crossAxisSpacing,
          padding: padding,
          staggeredTileBuilder: _getStaggeredTile,
        );
    }
  }

  StaggeredTile? _getStaggeredTile(int i) {
    return i >= tiles.length ? null : tiles[i];
  }

  TileWidget _getChild(BuildContext context, int index) {
    return  TileWidget(
        key:ValueKey(thinkDataList[index].id), thinkData:thinkDataList[index]);
  }
}

enum _StaggeredGridViewMode { count, extent }
